// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import Character from "../Character";
import ComponentBase from "../ComponentBase";
import Cspine from "./Cspine";


const { property, ccclass } = cc._decorator;
@ccclass
export class gif extends cc.Sprite {
  @property({ type: cc.SpriteAtlas })
  atlas1: cc.SpriteAtlas = null;
  @property({ type: cc.SpriteAtlas })
  atlas2: cc.SpriteAtlas = null;
  @property({ type: cc.SpriteAtlas })
  atlas3: cc.SpriteAtlas = null;
  @property(gif)
parent:gif=null
children:gif[]=[];
@property(String)
Bname:string="";
@property(Number)
scX:number=1;
timeArray:number[]=[70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70,70]
 kk:boolean=false
 ////////动画相关
  private currentIndex: number = 0;//目前的帧序号
  private playing: boolean = false;
  private timer: number = 0;
  private frameIntervalTime: number[] = [];//帧时长数组
  private startIndex: number = 0;
  private endIndex: number = 0;
  loop: number = 0; // 0: 停留在最后一帧, 1: 循环播放, 其他值: 正常播放

 //////

 //////颜色相关
brightness:number=1;//亮度
saturation:number=1;//饱和度
contrast:number=1;//对比度
//////////////////
  


pp(){this.setBrightness(-0.1)}



start(): void {
if(this.parent) { this.timeArray=this.parent.timeArray}

}




// 设置自身帧为空帧
   setEmptyFrame() {
    this.spriteFrame =null
   
    console.log(this.spriteFrame)
  }
  // 播放帧动画
  
  

 


  moveToNextFrame(currentIndex: number, endIndex: number,m:number) {
    
   
   
    if (currentIndex <= endIndex) {
        this.scheduleOnce(() => {
          
            this.timer = 0;
            this.changeF(currentIndex, 1);
            this.moveToNextFrame(currentIndex + 1, endIndex,m);
  
        }, this.timeArray[currentIndex]/1000);

    }else if(m){this.setEmptyFrame()}
}


moveTolastFrame(currentIndex: number, endIndex: number,m:number) {
    
   
   
  if (currentIndex >= endIndex) {
      this.scheduleOnce(() => {
        
          this.timer = 0;
          this.changeF(currentIndex, 1);
          this.moveTolastFrame(currentIndex - 1, endIndex,m);

      }, this.timeArray[currentIndex]/1000);

  }else if(m){this.setEmptyFrame()}
}
loopF(currentIndex: number, endIndex: number,m:number) {
    
   if(this.kk){
   
  if (currentIndex <=endIndex) {
    this.scheduleOnce(() => {
      
        this.timer = 0;
        this.changeF(currentIndex, 1);
        this.loopF(currentIndex +1, endIndex,m);

    }, this.timeArray[currentIndex]/1000);

}else {this.loopF(this.loop, endIndex, m);}
}

}
stop(){this.kk=false}
  changeF(index: number,tj:number) {
   let k
 
    switch (tj) {
      case 1:k=this.atlas1
        
        break;
        case 2:k=this.atlas2
        
        break;
        case 3:k=this.atlas3
        
        break;
    
      default:k=this.atlas1
        break;
    }
   if(index>=0){
    this.spriteFrame = k.getSpriteFrames()[index];}
    else this.setEmptyFrame();
    //console.log( this.spriteFrame.name);
  }



  moveSprite(x: number, y: number, speed: number, callback: () => void) {
    const dx = x - this.node.x;
    const dy = y -  this.node.y;
    const distance = Math.sqrt(dx * dx + dy * dy);
    const frames = distance / speed;
    const stepX = dx / frames;
    const stepY = dy / frames;
    function move() {
      this.node.x += stepX;
      this.node.y += stepY;
        if ((dx > 0 && this.node.x >= x) || (dx < 0 && this.node.x <= x) || (dy > 0 && this.node.y >= y) || (dy < 0 && this.node.y <= y)) {
          this.node.x = x;
          this.node.y = y;
            callback();
        } else {
            requestAnimationFrame(move);
        }
    }
    move();
}

















toBlue(){    
  console.log(this.node.color);
  let currentColor = this.node.color;
if(currentColor.b+12<255){
  this.node.color=new cc.Color(currentColor.r, currentColor.g, currentColor.b+12);

}else 
    this.node.color=new cc.Color(currentColor.r, currentColor.g, 255);


}

toRed(){  console.log(this.node.color);
  let currentColor = this.node.color;
if(currentColor.r+12<255){
  this.node.color=new cc.Color(currentColor.r+12, currentColor.g, currentColor.b);

}else 
    this.node.color=new cc.Color(255, currentColor.g, currentColor.b);}

    
    togreen(){  console.log(this.node.color);
  let currentColor = this.node.color;
if(currentColor.b+12<255){
  this.node.color=new cc.Color(currentColor.r, currentColor.g+12, currentColor.b);

}else 
    this.node.color=new cc.Color(currentColor.r, 255, currentColor.b);}

  
   

  // 修改精灵的亮度
  public setBrightness(value?: number) {
  
    if (value === undefined || value === null) {
      this.brightness = 1; // 恢复默认值
    } else {
      this.brightness +=value;
      
      // 增量修改，但不能超过上限和下限
    }
   
    this.getMaterial(0).setProperty('brightness', this.brightness);
   
  }


  // 修改精灵的透明度
  public setOpacity(value?: number) {
    if (value === undefined || value === null) {
      value = 255; // 恢复默认值
    } else {
      value = this.node.opacity + value; // 增量修改
    }
   
    this.node.opacity = value;
  }




// 修改精灵的饱和度
public setsaturation(value?: number) {
  
  if (value === undefined || value === null) {
    this.saturation = 1; // 恢复默认值
  } else {
     this.saturation += value;
    
    // 增量修改，但不能超过上限和下限
  }
  
  this.getMaterial(0).setProperty('saturation', this.saturation);
 
}



// 修改精灵的对比度
public setcontrast(value?: number) {
  
  if (value === undefined || value === null) {
    this.contrast = 1; // 恢复默认值
  } else {
     this.contrast += value;
    
    // 增量修改，但不能超过上限和下限
  }
  
  this.getMaterial(0).setProperty('contrast', this.contrast);
 
}


nodeBig(n:number,time:number) {

  cc.tween(this.node).to(time, {scaleX:n*this.scX,scaleY:n}).start();
  
}
























  rgbToHsv(r: number, g: number, b: number): [number, number, number] {
    r /= 255;
    g /= 255;
    b /= 255;
    const max = Math.max(r, g, b);
    const min = Math.min(r, g, b);
    let h: number, s: number, v: number = max;
    const d = max - min;
    s = max === 0 ? 0 : d / max;
    if (max === min) {
      h = 0;
    } else {
      switch (max) {
        case r:
          h = (g - b) / d + (g < b ? 6 : 0);
          break;
        case g:
          h = (b - r) / d + 2;
          break;
        case b:
          h = (r - g) / d + 4;
          break;
      }
      h /= 6;
    }
    return [h * 360, s * 100, v * 100];
  }

 

 
 
 
 
 
 
 
 
 
 
 
 
 

hsvToRgb(h: number, s: number, l: number): [number, number, number] {
    h /= 360;
    s /= 100;
    l /= 100;
    
    let r: number, g: number, b: number;
    
    if (s === 0) {
      r = g = b = l;
    } else {
      const hue2rgb = (p: number, q: number, t: number): number => {
        if (t < 0) t += 1;
        if (t > 1) t -= 1;
        if (t < 1 / 6) return p + (q - p) * 6 * t;
        if (t < 1 / 2) return q;
        if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
        return p;
      };
    
      const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
      const p = 2 * l - q;
    
      r = hue2rgb(p, q, h + 1 / 3);
      g = hue2rgb(p, q, h);
      b = hue2rgb(p, q, h - 1 / 3);
    }
    
    return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
  }












    transformSprite(action: number) {
        switch (action) {
            case 0:
                this.node.angle = 0; // 恢复默认状态
                break;
            case 8: // 旋转180度
                this.node.angle += 180;
                break;
            case 4: // 顺时针旋转
                this.node.angle += 90;
                break;
            case 6: // 逆时针旋转
                this.node.angle -= 90;
                break;
            case 456: // 垂直翻转
                this.node.scaleY *= -1;
                break;
            case 258: // 水平翻转
                this.node.scaleX *= -1;
                break;
            default:
                // 根据传入角度旋转
                this.node.angle += action;
                break;
        }
    }



    setB(n:number){

    
    
      
      }









}











 /*update(dt: number) {
    if (this.playing && this.frameIntervalTime.length > 0) {
      this.timer += dt;
      if (this.timer >= this.frameIntervalTime[this.currentIndex]) {
        this.timer = 0;
        this.currentIndex += 1;
        if (this.currentIndex > this.endIndex) {
          if (this.loop === 0) {
            this.currentIndex = this.endIndex; // 将当前帧的索引设置为最后一帧
            this.playing = false; // 停止动画
          } else if (this.loop === 1) {
            this.currentIndex = this.startIndex; // 将当前帧的索引设置为起始帧，实现循环播放
          } else {
            this.currentIndex = this.startIndex; // 将当前帧的索引设置为起始帧，实现正常播放
            // 处理其他值的(loop)逻辑
          }
        }
        if (this.currentIndex >= 0 && this.currentIndex < this.atlas.getSpriteFrames().length) {
          this.spriteFrame = this.atlas.getSpriteFrames()[this.currentIndex];
        }
      }
    }
  }*/


  



